trigger DiscussionForumAfterInsert on DiscussionForum__c bulk (after insert) {	
    if (!TeamUtil.currentlyExeTrigger) {
        try {
            DiscussionProfile__c defaultProfile = [select Id from DiscussionProfile__c where Name = 'DiscussionForum Administrator' limit 1];
            //Organizatrion Group 
            List<Group> go = [Select g.Type, g.Name from Group g where Type = 'Organization'];

            //Customer Portal Group            
            List<Group> portalGroup = new List<Group>();
            if(CreateNewDiscussionForumController.getAllowCustomerStatic()) 
            portalGroup = [Select g.Type, g.Name from Group g where Type = 'AllCustomerPortal'];
	           
            //Partner Portal Group
            List<Group> partnerGroup = new List<Group>();
            if(CreateNewDiscussionForumController.getAllowPartnerStatic())
            partnerGroup = [Select g.Type, g.Name from Group g where Type = 'PRMOrganization'];
            
            
            // build for bulk
            DiscussionForum__c[] t = Trigger.new;
            
            for (DiscussionForum__c team : Trigger.new) {
                /**
                * Group List To Insert.
                */
                List<Group> newGroups = new List<Group>();
                
                /**
                * Create Sharing Group for current Team.
                */      
                Group g = new Group();
                g.Name = 'discussionForumSharing' + team.Id;
                insert g;
                
                if(team.PublicProfile__c != null || team.NewMemberProfile__c != null){
                    
                    //Create GroupMember for everyone member
                    GroupMember gm = new GroupMember();
                    gm.GroupId = g.Id;
                    gm.UserOrGroupId = go[0].Id;
                    insert gm;
                    
					//If Customer Portal group exist add GroupMember
					if(portalGroup.size() > 0 ){
						GroupMember gmPortal = new GroupMember();
	                    gmPortal.GroupId = g.Id;
	                    gmPortal.UserOrGroupId = portalGroup[0].Id;
	                    insert gmPortal;
					}                

					//If Partner Portal group exist add GroupMember
					if(partnerGroup.size() > 0 ){
						GroupMember gmPortal = new GroupMember();
	                    gmPortal.GroupId = g.Id;
	                    gmPortal.UserOrGroupId = partnerGroup[0].Id;
	                    insert gmPortal;
					}  
                }   
                        
                /* ### Create Queues ###*/
                    
                // Create DiscussionForum Queue
                Group gdqw = new Group();
                gdqw.Type = 'Queue';
                gdqw.Name = 'DiscussionForum' + team.Id;
                insert gdqw;
                
                String discussionForumQueueId = gdqw.id;
                
                // ### Allow SObjects to be managed by recently created queues ###
                List<QueueSobject> sobjectsQueueAllowed = new List<QueueSobject>();

				// Discussion allows
				QueueSobject allowForum = new QueueSobject(SobjectType = Schema.SObjectType.DiscussionForum__c.getName() ,QueueId = discussionForumQueueId);
			   	sobjectsQueueAllowed.add(allowForum);
				
				QueueSobject allowTopics = new QueueSobject(SobjectType = Schema.SObjectType.DiscussionTopic__c.getName() ,QueueId = discussionForumQueueId);
			   	sobjectsQueueAllowed.add(allowTopics);
			   	
			   	QueueSobject allowMessages = new QueueSobject(SobjectType = Schema.SObjectType.DiscussionMessage__c.getName() ,QueueId = discussionForumQueueId);
			   	sobjectsQueueAllowed.add(allowMessages);
						
			   	// DiscussionForum
				QueueSobject allowTeams = new QueueSobject(SobjectType = Schema.SObjectType.DiscussionForum__c.getName() ,QueueId = discussionForumQueueId);
			   	sobjectsQueueAllowed.add(allowTeams);
			   	
			   	QueueSobject allowTeamMembers = new QueueSobject(SobjectType = Schema.SObjectType.DiscussionForum__c.getName() ,QueueId = discussionForumQueueId);
			   	sobjectsQueueAllowed.add(allowTeamMembers);	
			   	
			   	//Insert all the allowed sobjects 
                upsert sobjectsQueueAllowed;

                //Upsert Team owner
                DiscussionForum__c tempTeam = [select ownerId, Id, Name from DiscussionForum__c where Id =: team.Id limit 1];
                tempTeam.ownerId = discussionForumQueueId;
                // We set this to true becuase we dont want all the minifeed triggers and update 
                // triggers firing off when all we want to do is update the owner id.
                TeamUtil.currentlyExeTrigger = true;
                
                update tempTeam;
                TeamUtil.currentlyExeTrigger = false;
                        
                /**
                * Create __Shared object for team
                */
                DiscussionForum__Share teamS = new DiscussionForum__Share();
                teamS.ParentId = team.Id;
                teamS.UserOrGroupId = g.Id;
                teamS.AccessLevel = 'Read';
                teamS.RowCause = 'Manual';
                insert teamS;  
            
                /**
                * Create the first team member (the founder)
                */
                
                DiscussionMember__c firstTeamMember = new DiscussionMember__c();
                firstTeamMember.User__c = Userinfo.getUserId();
                firstTeamMember.Name = UserInfo.getName();
                firstTeamMember.DiscussionForum__c = team.Id;
                firstTeamMember.DiscussionProfile__c = defaultProfile.Id;
                insert firstTeamMember;
                
                
            }
        } finally {
            TeamUtil.currentlyExeTrigger = false;
        }
    } else {
        DiscussionProfile__c defaultProfile = [select Id from DiscussionProfile__c where Name = 'DiscussionForum Administrator' limit 1];
        for (DiscussionForum__c team : Trigger.new) {
            DiscussionMember__c firstTeamMember = new DiscussionMember__c();
            firstTeamMember.User__c = Userinfo.getUserId();
            firstTeamMember.Name = UserInfo.getName();
            firstTeamMember.DiscussionForum__c = team.Id;
            firstTeamMember.DiscussionProfile__c = defaultProfile.Id;
            insert firstTeamMember; 
        }
    }
}